package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/10/20 2:37 PM
 *
 * https://leetcode-cn.com/problems/longest-palindromic-substring/
 */
public class _5_最长回文子串_03 {

    public String longestPalindrome(String s) {

        if (s == null) {

            return null;
        }

        if (s.length() == 1) {

            return s;
        }

        char[] sChar = s.toCharArray();

        int index=0,len = 1;

        int i = 0;
        int left,right;
        while (i < sChar.length) {

            left = i - 1;
            right = i + 1;
            while (right < sChar.length && sChar[right - 1] == sChar[right]) {

                right++;
            }

            while (left >= 0 && right < sChar.length && sChar[left] == sChar[right]) {

                left--;
                right++;
            }

            int clen = right - left - 1;
            if ( clen > len) {

                len = clen;
                index = left+1;
            }

            i = right;
        }

        return s.substring(index,index+len);
    }

}
